/*
Copyright (c) 2009, Dan Hagerstrand and contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Cirrus Creative nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef BENG_SYSTEM_H
#define BENG_SYSTEM_H

#include <cstdio>
#include <ctime>
#include <list>
#include <vector>

#include "BengInterface.h"
#include "BengVersion.h"
#include "CString.h"
#include "Resource.h"
#include "Scene.h"

class BengSystem {
    friend class BengScene;
    friend class BengIGfx;
    friend class BengInterface;
    friend class BengCamera;

    private:

    int exitLoop_;

    protected:

    FILE *logFile;

    char gameIdent[16];
    BengCString gameName;
    int windowW;
    int windowH;
    short int framerate;
    short int frameskip;
    short int maxFrametime;

    public:

    BengScene *scene;
    BengResourceList<BengISprite> *spriteResourceList;
    BengResourceList<BengISample> *sampleResourceList;

    // These functions are defined first to avoid crashes/reference errors.
    // Move these and demons will fly out your nose.
    void consprint(const char *message);
    void consprintf(const char *format, ...);

    BengInterface *interface;

    BengSystem(void)
    {
        //Initiate time variables
        struct tm *local;
        time_t t;

        this->interface = new BengInterface(this);
        this->spriteResourceList = new BengResourceList<BengISprite>(this);
        this->sampleResourceList = new BengResourceList<BengISample>(this);

        t = time(NULL);

        local = localtime(&t);

        logFile = fopen("log.txt", "wt");

        this->consprintf("\nBlitzzEngine %s"
        "\n\nA Framework for 2D Games"
        "\nDeveloped by Cirrus Creative"
        "\n\nThird Party Library Credits:"
        "\n- Fixed Point Math Routines by Markus Trenkwalder"
        "\n    Thanks for that! You rule!"
        "\n----------------------------------------\n"
        "Log time: %s----------------------------------------\n\n", BENG_VERSION_STRING,
        asctime(local));

        this->scene = NULL;
        this->exitLoop_ = 0;

        // TODO: Config of sorts
        // For now, adding in temporary hard-coded resolution

        this->windowW = 640;
        this->windowH = 480;
        this->framerate = 60;
        this->frameskip = 9;
        this->gameName.set("BlitzzEngine Game");
    }
    ~BengSystem(void);

    void giveError(int severity, const char *format, ...);

    const char *getName(void);
    void setName(const char *name);

    void getWindowSize(int &xOut, int &yOut);

    int newScene(const char *scenename);

    int mainLoop(void);

};

#endif
